Git Kata 2: Branches

Learn about listing, creating, and working with branches.

Branching is one of the most used and useful features of Git. Commits are always applied to a branch. Creating a new branch in a local repository starts a new commit history, starting from a specific commit in the source branch from which the new branch is created.

Branches are used to isolate work. New work, such as features and hotfixes, are created on a new local branch. These are referred to as feature or topic branches. Branches keep work organized and prevent new work from breaking existing work. Developers can create new local branches at will and commit changes to them. Branches can then be discarded or pushed to a remote server and merged into other branches.

Step 1: Listing and creating branches#

The command to list all the branches is given below.

The output will be something like this:

Listing all the branches

Command's Parameter

Command / Parameter

Description

branch

When executed with no other parameters, this lists all the branches.

The git branch command lists all the branches in the local repository. Git repositories are initialized with one branch, master (also being referred to as main in this chapter). The current branch is highlighted and prepended with a star.

The command to create a new branch is given below.

The result after executing the command will be something like this:

Creating a new branch named newbranch

Command's Parameters

Command / Parameter

Description

branch

This executes commands related to branches.

newbranch

This is the name of the new branch to create.

When combined with a branch name, the branch command creates a new branch. When successful, this command produces no output.

e137e9b..
first commit
e137e9b.....
28df86f..
Reordered List
28df86f.....
823c7c8.. Another reorder
823c7c8.. Anoth...
main
main
HEAD
HEAD
newbranch
newbranch
Viewer does not support full SVG 1.1
The visualization now displays three labels on the most recent commit: main, HEAD, and newbranch

Note: The git branch newbranch command creates a new branch, which is another type of ref. Remember that each commit is always applied to a branch. So far, all the commits have been applied to main. Now that a new branch has been added, commits can be applied to either branch.

The command to list all the branches is given below.

The output will be something like this:

Listing all the branches

Command's Parameter

Command / Parameter

Description

branch

When executed with no other parameters, this lists all the branches.

Executing git branch again shows that the newbranch branch has been created, and that main is still the current branch.

Step 2: Switch between branches#

The command to switch to newbranch is given below.

When we run the command above, the output will be something like this:

Switching to the new branch

Command's Parameters

Command / Parameter

Description

checkout

This switches to a designated branch when combined with a branch name.

newbranch

This indicates the branch to be checked out.

The checkout command can act on two repository objects: commits and branches. This execution combines checkout with newbranch. This makes newbranch the current branch.

Note: Checkout is something quite different in CVCS systems such as Subversion, Clearcase, or Team Foundation Server. The git checkout command does something quite different: it checks out an object (a commit or a branch) from the local index and applies it to the working directory. Git doesn’t have the concept of an exclusive checkout.

e137e9b..
first commit
e137e9b.....
28df86f..
Reordered List
28df86f.....
823c7c8..
Another reorder
823c7c8.....
main
main
HEAD
HEAD
newbranch
newbranch
Viewer does not support full SVG 1.1
The visualization now shows that the HEAD ref has been moved to point to newbranch

The command to list all the branches is given below.

The result will be something like this:

Listing all the branches

Command's Parameter

Command / Parameter

Description

branch

When executed with no other parameters, this lists all the branches.

This command lists the local branches again. We now see that newbranch is the current (checked out) branch.

To swap lines in the file and save:

  • Switch to the text editor.
  • Swap the first and last lines of storelist.txt.
  • Save the changes.
Swapping the first and last lines in the file

This step makes a new change to the storelist.txt file.

The command to get the repository status is given below.

The output of the command above will be something like this:

Getting the repository status

Command's Parameter

Command / Parameter

Description

status

This shows the status of all the files in the repository.

The git status command shows the state of the repository. The first line shows that the current branch is newbranch. The change we just made to storelist.txt has not been staged or committed.

The status output also explains that we could, if desired, use the git checkout command to discard the changes in the working directory. Remember that git checkout is used to pull changes out of the index and apply them to the working directory. If we were to do that now, storelist.txt would be overwritten in the working directory and the change would be discarded. The storelist.txt file in the working directory would be updated to match what’s in the index. The output of this command is:

Output

Message

Meaning

"On branch newbranch"

This is the current branch.

"Changes not staged for commit: (use git add <file>...” to update what will be committed) (use git checkout --<file>... to discard changes in working directory)"



The list of changes below this message haven't been staged for commit.

"modified: storelist.txt"

The red text indicates that change to storelist.txt hasn't been staged.

"no changes added to commit (use git add and/or git commit -a)"

There are changes in the working directory that must be staged in order to be included in the next commit.

Step 3: Commit to a branch#

The command to commit the changes to the repository is given below.

The output will be something like this:

Commiting the changes to the repository

Command's Parameters

Command / Parameter

Description

commit

This saves the current state of staged files in a new commit.

-a

This stages all the modified files.

-m

This designates a commit message on the command line.

"First line last"

This is the commit message associated with the new commit.

This new commit is applied to newbranch.

The output of the command is:

Output

Message

Meaning

"newbranch"

This is the branch to which the commit was applied.

"1e19013"

These are the first seven characters of the commit hash.

“First line last”

This is the commit message.

"1 file changed, 2 insertions(+), 2 deletions(-)

This is the number of files changed and the number of lines inserted and deleted.

e137e9b...
first commit
e137e9b......
28df86f..
Reordered List
28df86f.....
823c7c8..
Another reorder
823c7c8.....
main
main
HEAD
HEAD
newbranch
newbranch
61dd33f..
First line last
61dd33f.....
Viewer does not support full SVG 1.1
This is the first commit to a branch other than main

Note: This is the first commit to a branch other than main. The new commit has been applied to newbranch. The HEAD ref points to the new commit on newbranch.

The command to switch to the main branch is given below.

After the execution of the command above, the output will be something like this:

Switching to the main branch

Command's Parameters

Command / Parameter

Description

checkout

This checks out a branch or commit.

master

This is the name of the branch to checkout.

This command switches the current branch back to main.

HEAD
HEAD
e137e9b...
first commit
e137e9b......
28df86f..
Reordered List
28df86f.....
823c7c8..
Another reorder
823c7c8.....
main
main
newbranch
newbranch
61dd33f..
First line last
61dd33f.....
Viewer does not support full SVG 1.1
The Git checkout main command has checked out the main branch

Note: For the first time, HEAD doesn’t point to the most recent commit. It points to the last commit made on the main branch. This is an example of how Git moves the HEAD ref to track where new commits will be added.

To switch to the text editor and reload:

  • Switch to the text editor.
  • Click “Reload.”
  • Note that the file has changed.
Text editor in main branch

The previous command, git checkout master, switched to the main branch. This checks out the main branch from the local index, which updates the working directory to the state saved in main. Returning to the text editor, we see that the storelist.txt has changed. The file is now back to its previous state, as it is in the main branch. The storelist.txt file now has two versions: one in main and another in newbranch. The next kata will demonstrate how the commits from one branch can be applied to another branch.

Practice commands#

We’ve given a terminal and a table containing a list of commands discussed in this lesson. Try out these commands after running the terminal, and check out the results!

Commands

Step

Command

This lists all the branches.

git branch

This creates a new branch called newbranch.

git branch newbranch

This lists all the branches.

git branch

This switches to newbranch.

git checkout newbranch

This lists all the branches.

git branch

This swaps the first and last lines of storelist.txt and saves the file.

nano storelist.txt

This gets the status of the repository.

git status

This commits the change to the repository, setting the commit comment from the command line.


git commit -a -m "First line last"

This switches to the master/main branch.

git checkout master

This returns to the text editor and reloads the storelist.txt file.

nano storelist.txt

Terminal 1
Terminal

Click to Connect...

Git Kata 1: New Local Repository

Git Kata 3: Merging